package com.jarpackage.ui;

import com.jarpackage.config.AppConfig;
import com.jarpackage.config.RepositoryConfig;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.IOException;

public class SettingsDialog extends Dialog<Void> {
    private final AppConfig appConfig;

    public SettingsDialog(Stage owner, AppConfig appConfig) {
        this.appConfig = appConfig;

        initOwner(owner);
        setTitle("Settings");

        // 创建对话框内容
        VBox content = new VBox(10);
        content.setPadding(new Insets(10));

        // 设置标签样式
        String labelStyle = "-fx-font-size: 16px; -fx-text-fill: #333; -fx-font-weight: bold;";
        Label repositoryLabel = new Label("Maven Repository:");
        repositoryLabel.setStyle(labelStyle);
        content.getChildren().add(repositoryLabel);

        GridPane repositoryForm = new GridPane();
        repositoryForm.setHgap(10);
        repositoryForm.setVgap(5);
        repositoryForm.setPadding(new Insets(5));

        Label urlLabel = new Label("URL:");
        Label usernameLabel = new Label("Username:");
        Label passwordLabel = new Label("Password:");
        
        urlLabel.setStyle(labelStyle);
        usernameLabel.setStyle(labelStyle);
        passwordLabel.setStyle(labelStyle);

        TextField urlField = new TextField();
        TextField usernameField = new TextField();
        PasswordField passwordField = new PasswordField();

        if (!appConfig.getRepositories().isEmpty()) {
            RepositoryConfig repo = appConfig.getRepositories().get(0);
            urlField.setText(repo.getUrl());
            usernameField.setText(repo.getUsername());
            passwordField.setText(repo.getPassword());
        }

        repositoryForm.add(new Label("URL:"), 0, 0);
        repositoryForm.add(urlField, 1, 0);
        repositoryForm.add(new Label("Username:"), 0, 1);
        repositoryForm.add(usernameField, 1, 1);
        repositoryForm.add(new Label("Password:"), 0, 2);
        repositoryForm.add(passwordField, 1, 2);

        content.getChildren().add(repositoryForm);

        // 创建按钮
        ButtonType saveButtonType = new ButtonType("保存", ButtonBar.ButtonData.OK_DONE);
        ButtonType cancelButtonType = new ButtonType("取消", ButtonBar.ButtonData.CANCEL_CLOSE);
        getDialogPane().getButtonTypes().addAll(saveButtonType, cancelButtonType);

        // 设置按钮栏样式
        DialogPane dialogPane = getDialogPane();
        dialogPane.getStyleClass().add("dialog-pane");
        dialogPane.setStyle("-fx-alignment: CENTER;");

        // 移除 settings 相关代码
        setResultConverter(dialogButton -> {
            if (dialogButton == saveButtonType) {
                // 保存仓库配置
                RepositoryConfig repositoryConfig = new RepositoryConfig();
                repositoryConfig.setUrl(urlField.getText().trim());
                repositoryConfig.setUsername(usernameField.getText().trim());
                repositoryConfig.setPassword(passwordField.getText());

                appConfig.getRepositories().clear();
                appConfig.getRepositories().add(repositoryConfig);

                try {
                    appConfig.save("config.json");
                } catch (IOException ex) {
                    Alert alert = new Alert(Alert.AlertType.ERROR);
                    alert.setTitle("Error");
                    alert.setHeaderText(null);
                    alert.setContentText("Failed to save settings: " + ex.getMessage());
                    alert.showAndWait();
                }
            }
            return null;
        });

        getDialogPane().setContent(content);
    }
}